﻿using UnityEngine;
using System.Collections;

using System.Collections.Generic;


using System.Runtime.InteropServices;
using System.Text;
using System.IO;
using UnityEngine.UI;

public class MainControl : MonoBehaviour 
{

    enum _XOscDataType : byte
    {
        OSC_TYPE_INT = 0,
        OSC_TYPE_FLOAT = 1,
        OSC_TYPE_STRING = 2,
        OSC_TYPE_UNKNOWN = 3,
    };



    [DllImport("OSC_Test")]
    private static extern void setup(int port);
    [DllImport("OSC_Test")]
    private static extern void release();
    [DllImport("OSC_Test")]
    private static extern bool haveMessage();
    [DllImport("OSC_Test")]
    private static extern void getAMessage();
    [DllImport("OSC_Test")]
    private static extern int getAddrLen();
    [DllImport("OSC_Test", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.None)]
    private static extern void getAddr(StringBuilder str);
    [DllImport("OSC_Test")]
    private static extern int getDataSum();
    [DllImport("OSC_Test")]
    private static extern _XOscDataType getDataType(int index);
    [DllImport("OSC_Test")]
    private static extern int getDataAsInt(int index);
    [DllImport("OSC_Test")]
    private static extern float getDataAsFloat(int index);
    [DllImport("OSC_Test")]
    private static extern int getDataAsStringLen(int index);
    [DllImport("OSC_Test", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.None)]
    private static extern void getDataAsString(StringBuilder str, int index);

    enum GameState
    {
        m_WaitStart,
        m_InGame,
        m_Result,

    };


    public string filename;
    public GameObject m_UIstart;
    public GameObject m_UITip;
    public GameObject m_UIdotNum;
    public GameObject m_UIInGame;
    public GameObject m_UIEnd;

    public GameObject m_ObjInUITime;
    public GameObject m_ObjInUICoin;
    public GameObject m_ObjInUIZangAi;

    public GameObject m_ObjLastUITime;
    public GameObject m_ObjLastUICoin;
    public GameObject m_ObjLastUIZangAi;

    public GameObject m_ObjMap;
    public GameObject m_ObjDir;

    public GameObject m_CoinRoot;

    private GameObject m_ObjCamera;
    private GameObject m_ObjPlayer;

    private float m_GameStartTime =0;
    private int m_GameTimeHour;
    private int m_GameTimeMinute;
    private int m_GameTimeSecond;

    private int m_GameGoldCoin;
    private int m_GameZangAi;


    private float m_TimeStart;
    private float m_TimeEnd;

    private GameState m_GameState = GameState.m_WaitStart;

    private List<GameObject> m_ListCoins;
    private List<GameObject> m_ListZangAis;


    //// test

    private float valueMap = 0.0f;

	// Use this for initialization
	void Start ()
    {
        setup(12345);
        SetShowUI(3);

        StartCoroutine(loadScenee());

        m_ObjCamera = GameObject.Find("Camera");
        m_ObjPlayer = GameObject.Find("player");



	}

    void CoinInit()
    {
        m_ListCoins = new List<GameObject>();
        m_ListZangAis = new List<GameObject>();

        int CoinNun = m_CoinRoot.transform.childCount;

        for (int i = 0; i < CoinNun; ++i)
        {
            GameObject temp = m_CoinRoot.transform.GetChild(i).gameObject;
            m_ListCoins.Add(temp);

        }
    
    }


    IEnumerator loadScenee()
    {
        string path;
        path = "file://" + Application.dataPath + "/StreamingAssets/" + filename + ".unity3d";
        Debug.Log(path);
        WWW www = new WWW(path);
        yield return www;
        AssetBundle bundle = www.assetBundle;
      
        GameObject ObjScene = Instantiate(www.assetBundle.mainAsset) as GameObject;
        bundle.Unload(false);

    }



    void InitCoins()
    {
        m_GameTimeHour = 0;
        m_GameTimeMinute = 0;
        m_GameTimeSecond = 0;

        m_GameGoldCoin = 0;
        m_GameZangAi = 0;
    }


	// Update is called once per frame
	void Update ()
    {
        valueMap += 0.003f;
        timeUpdate();

        setMap(valueMap);

	}

    void timeUpdate()
    {
        float tempTime = Time.time;
       

        int timeL = (int)(tempTime - m_GameStartTime)*10;

        m_GameTimeSecond = timeL % 60;
        m_GameTimeMinute = timeL / 60;
        m_GameTimeHour = m_GameTimeMinute / 60;
        m_GameTimeMinute = m_GameTimeMinute % 60;

       // Debug.Log(":hour:"+ m_GameTimeHour +":minute:"+ m_GameTimeMinute+ ":second:"+ m_GameTimeSecond);

        SetUIs();
    
    }


    void FixedUpdate()
    {

        switch (m_GameState)
        {
            case GameState.m_WaitStart:
                { 
                
                }

                break;
            case GameState.m_InGame:
                { 
                
                }
                break;
            case GameState.m_Result:
                { 
                
                }
                break;
            default:
                break;
        
        
        }

        NetServerUpdateWorking();

    }

    void SetUIs()
    {
        string coinNumstr = m_GameGoldCoin.ToString();
        string zanghaistr = m_GameZangAi.ToString();
        string timestr = m_GameTimeHour.ToString() + ":"+m_GameTimeMinute.ToString() +":"+ m_GameTimeSecond.ToString();


        m_ObjInUITime.GetComponent<Text>().text = timestr;
        m_ObjInUICoin.GetComponent<Text>().text = coinNumstr;
        m_ObjInUIZangAi.GetComponent<Text>().text = zanghaistr;

        m_ObjLastUITime.GetComponent<Text>().text = timestr;
        m_ObjLastUICoin.GetComponent<Text>().text = coinNumstr;
        m_ObjLastUIZangAi.GetComponent<Text>().text = zanghaistr;
    
    }


    void SetShowUI(int n)
    {
        m_UIstart.SetActive(false);
        m_UITip.SetActive(false);
        m_UIdotNum.SetActive(false);
        m_UIInGame.SetActive(false);
        m_UIEnd.SetActive(false);


        switch (n)
        {
            case 0:
                {
                    m_UIstart.SetActive(true);
                
                }
                break;
            case 1:
                {
                    m_UITip.SetActive(true);
                                
                }
                break;
            case 2:
                {
                    m_UIdotNum.SetActive(true);
                }
                break;
            case 3:
                {
                    m_UIInGame.SetActive(true);
                }
                break;
            case 4:
                {
                    m_UIEnd.SetActive(true);
                }
                break;

            default:
                break;
        
        }
    
    
    }

    void setMap(float f)
    {

        if (f > 1.0f)
        {
            f = 1.0f;
        }

        if (f < 0.0f)
        {
            f = 0.0f;
        }
        //64 -294

        float dirH = 64.0f + (294.0f - 64.0f) * f;

       

        Vector3 mappos = m_ObjMap.GetComponent<RectTransform>().position;
       

        Vector3 dirPos = new Vector3(mappos.x,dirH,mappos.z);

        m_ObjDir.GetComponent<RectTransform>().position = dirPos;

        Debug.Log(mappos);
        Debug.Log(dirPos);

    
    }


    void NetServerUpdateWorking()
    {
        if (haveMessage())
        {
            getAMessage();
            StringBuilder buffer = new StringBuilder(getAddrLen());
            getAddr(buffer);
            //print(buffer);

            string bufferstring = buffer.ToString();
            Debug.Log("log:"+ bufferstring);
            if (string.Compare(bufferstring, "/go") == 0)
            {
                for (int i = 0; i < getDataSum(); ++i)
                {
                    switch (getDataType(i))
                    {
                        case _XOscDataType.OSC_TYPE_INT:
                            int numt = getDataAsInt(i);
                           
                            break;

                    }
                }
            }

            string m_shutdownstring = buffer.ToString();
            string shutdownstring = "/shutdown";
            if (string.Compare(m_shutdownstring, shutdownstring) == 0)
            {
                for (int i = 0; i < getDataSum(); ++i)
                {
                    switch (getDataType(i))
                    {
                        case _XOscDataType.OSC_TYPE_INT:
                            print(getDataAsInt(i));
                           
                            break;

                    }
                }
            }

            string m_Wheelstring = buffer.ToString();
            if (string.Compare(m_Wheelstring, "/wheel") == 0)
            {
                for (int i = 0; i < getDataSum(); ++i)
                {
                    switch (getDataType(i))
                    {
                        case _XOscDataType.OSC_TYPE_INT:
                            print(getDataAsInt(i));
                          
                            break;

                    }
                }
            }

            string m_sleepstring = buffer.ToString();
            if (string.Compare(m_sleepstring, "/btnsleep") == 0)  //如果待机按钮被按下，返回主界面
            {
                for (int i = 0; i < getDataSum(); ++i)
                {
                    switch (getDataType(i))
                    {
                        case _XOscDataType.OSC_TYPE_INT:

                           

                            break;

                    }
                }
            }

        }

    }

}
